package com.zqxx.examine.management.controller.page.teacher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.common.utils.PagingUtil;
import com.zqxx.examine.common.utils.QuestionAdapter;
import com.zqxx.examine.management.service.QuestionService;
import com.zqxx.examine.model.question.Field;
import com.zqxx.examine.model.question.Question;
import com.zqxx.examine.model.question.QuestionFilter;
import com.zqxx.examine.model.question.QuestionQueryResult;

@Controller
public class QuestionPageTeacher
{
  @Autowired
  private QuestionService questionService;
  
  @RequestMapping(value={"/teacher/question/question-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String questionListPage(Model model)
  {
    return "redirect:question-list/filter-0-0-0-0-0-1.html";
  }
  
  @RequestMapping(value={"/teacher/question/question-list/filter-{fieldId}-{knowledge}-{questionType}-{tag}-{searchParam}-{page}.html"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String questionListFilterPage(Model model, @PathVariable("fieldId") int fieldId, @PathVariable("knowledge") int knowledge, @PathVariable("questionType") int questionType, @PathVariable("tag") int tag, @PathVariable("searchParam") String searchParam, @PathVariable("page") int page)
  {
    QuestionFilter qf = new QuestionFilter();
    qf.setFieldId(fieldId);
    qf.setKnowledge(knowledge);
    qf.setQuestionType(questionType);
    qf.setTag(tag);
    if (searchParam.equals("0")) {
      searchParam = "-1";
    }
    qf.setSearchParam(searchParam);
    
    Page<Question> pageModel = new Page();
    pageModel.setPageNo(page);
    pageModel.setPageSize(20);
    

    List<Question> questionList = this.questionService.getQuestionList(pageModel, qf);
    

    String pageStr = PagingUtil.getPageBtnlink(page, pageModel
      .getTotalPage());
    
    List<Field> fieldList = this.questionService.getAllField(null);
    model.addAttribute("fieldList", fieldList);
    


    model.addAttribute("knowledgeList", this.questionService
      .getKnowledgePointByFieldId(fieldId, null));
    
    model.addAttribute("questionTypeList", this.questionService
      .getQuestionTypeList());
    
    model.addAttribute("questionFilter", qf);
    model.addAttribute("questionList", questionList);
    
    model.addAttribute("pageStr", pageStr);
    model.addAttribute("tagList", this.questionService.getTags(null));
    
    model.addAttribute("fieldId", Integer.valueOf(fieldId));
    model.addAttribute("knowledge", Integer.valueOf(knowledge));
    model.addAttribute("questionType", Integer.valueOf(questionType));
    model.addAttribute("searchParam", "-1".equals(searchParam) ? "" : searchParam);
    
    return "question-list";
  }
  
  @RequestMapping(value={"/teacher/question/question-list/filterdialog-{fieldId}-{knowledge}-{questionType}-{searchParam}-{page}.html"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String questionListFilterDialogPage(Model model, @PathVariable("fieldId") int fieldId, @PathVariable("knowledge") int knowledge, @PathVariable("questionType") int questionType, @PathVariable("searchParam") String searchParam, @PathVariable("page") int page)
  {
    QuestionFilter qf = new QuestionFilter();
    qf.setFieldId(fieldId);
    qf.setKnowledge(knowledge);
    qf.setQuestionType(questionType);
    if (searchParam.equals("0")) {
      searchParam = "-1";
    }
    qf.setSearchParam(searchParam);
    
    Page<Question> pageModel = new Page();
    pageModel.setPageNo(page);
    pageModel.setPageSize(20);
    
    List<Question> questionList = this.questionService.getQuestionList(pageModel, qf);
    

    String pageStr = PagingUtil.getPageBtnlink(page, pageModel
      .getTotalPage());
    
    model.addAttribute("fieldList", this.questionService.getAllField(null));
    
    model.addAttribute("knowledgeList", this.questionService
      .getKnowledgePointByFieldId(fieldId, null));
    
    model.addAttribute("questionTypeList", this.questionService
      .getQuestionTypeList());
    
    model.addAttribute("questionFilter", qf);
    model.addAttribute("questionList", questionList);
    model.addAttribute("pageStr", pageStr);
    

    model.addAttribute("fieldId", Integer.valueOf(fieldId));
    model.addAttribute("knowledge", Integer.valueOf(knowledge));
    model.addAttribute("questionType", Integer.valueOf(questionType));
    model.addAttribute("searchParam", searchParam);
    model.addAttribute("tagList", this.questionService.getTags(null));
    return "question-list-dialog";
  }
  
  @RequestMapping(value={"/teacher/question/question-add"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String questionAddPage(Model model)
  {
    List<Field> fieldList = this.questionService.getAllField(null);
    model.addAttribute("fieldList", fieldList);
    return "question-add";
  }
  
  @RequestMapping(value={"/teacher/question/question-import"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String questionImportPage(Model model)
  {
    List<Field> fieldList = this.questionService.getAllField(null);
    model.addAttribute("fieldList", fieldList);
    return "question-import";
  }
  
  @RequestMapping(value={"/teacher/question/question-preview/{questionId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String questionPreviewPage(Model model, @PathVariable("questionId") int questionId, HttpServletRequest request)
  {
    String strUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + "/";
    Question question = this.questionService.getQuestionByQuestionId(questionId);
    List<Integer> idList = new ArrayList();
    idList.add(Integer.valueOf(questionId));
    List<QuestionQueryResult> questionQueryList = this.questionService.getQuestionDescribeListByIdList(idList);
    HashMap<Integer, QuestionQueryResult> questionMap = new HashMap();
    for (QuestionQueryResult qqr : questionQueryList) {
      if (questionMap.containsKey(Integer.valueOf(qqr.getQuestionId())))
      {
        QuestionQueryResult a = (QuestionQueryResult)questionMap.get(Integer.valueOf(qqr.getQuestionId()));
        questionMap.put(Integer.valueOf(qqr.getQuestionId()), a);
      }
      else
      {
        questionMap.put(Integer.valueOf(qqr.getQuestionId()), qqr);
      }
    }
    QuestionAdapter adapter = new QuestionAdapter(question, null, (QuestionQueryResult)questionMap.get(Integer.valueOf(questionId)), strUrl);
    String strHtml = adapter.getStringFromXML(true, false, true);
    model.addAttribute("strHtml", strHtml);
    model.addAttribute("question", question);
    return "question-preview";
  }
}

